#!/usr/bin/python3
# coding=utf-8

# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# This file is a part of the CANN Open Software.
# Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
# Please refer to the License for details. You may not use this file except in compliance with the License.
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
# See LICENSE in the root of the software repository for the full text of the License.
# ======================================================================================================================
import os
import sys
import logging

import numpy as np
import tensorflow as tf

IS_OUTPUT_TXT = False


class MatmulGenData:
    def __init__(self, m, n, k, b, is_trans_a, is_trans_b, is_bias,\
        data_type_str):
        self.m = m
        self.n = n
        self.k = k
        self.b = b
        self.is_trans_a = is_trans_a
        self.is_trans_b = is_trans_b
        self.is_bias = is_bias
        self.data_type_str = data_type_str

    def gen_golden_data_fp16_fp32(self, work_dir):
        src_type = np.float16
        dst_type = np.float32
        bias_gm = np.random.uniform(-1, 1, [1, self.n]).astype(dst_type)

        for idx in range(self.b):
            x1_gm_left = np.random.uniform(-1, 1, [self.m, self.k]).astype(src_type)
            x2_gm_right = np.random.uniform(-1, 1, [self.k, self.n]).astype(src_type)
            golden_one = np.matmul(x1_gm_left.astype(dst_type), x2_gm_right.astype(dst_type)).astype(dst_type)
            if self.is_bias:
                golden_one = golden_one + bias_gm.astype(dst_type)
            if self.is_trans_a:
                x1_tmp = x1_gm_left.transpose()
            else:
                x1_tmp = x1_gm_left
            if self.is_trans_b:
                x2_tmp = x2_gm_right.transpose()
            else:
                x2_tmp = x2_gm_right
            if idx == 0:
                x1_gm = x1_tmp
                x2_gm = x2_tmp
                golden = golden_one
            else:
                x1_gm = np.vstack((x1_gm, x1_tmp))
                x2_gm = np.vstack((x2_gm, x2_tmp))
                golden = np.vstack((golden, golden_one))

        x1_gm.tofile(work_dir + "/input/x1_gm.bin")
        x2_gm.tofile(work_dir + "/input/x2_gm.bin")
        if self.is_bias:
            bias_gm.tofile(work_dir + "/input/bias_gm.bin")
        golden.tofile(work_dir + "/output/golden.bin")

        return 0

    def gen_golden_data(self, work_dir):
        if self.data_type_str == "float16_float32":
            self.gen_golden_data_fp16_fp32(work_dir)
        else:
            logging.info("[ERROR] can't support data type %s" % (self.data_type_str))
            return -1
        return 0

    def gen_fake_golden_data(self, work_dir):
        data_type_bytes_ab = 2 # float16
        data_type_bytes_c = 4  # float32

        file_byte = self.b * self.m * self.k * data_type_bytes_ab
        with open(work_dir + "/input/x1_gm.bin", 'wb') as file:
            file.truncate(file_byte)

        file_byte = self.b * self.k * self.n * data_type_bytes_ab
        with open(work_dir + "/input/x2_gm.bin", 'wb') as file:
            file.truncate(file_byte)

        if self.is_bias:
            file_byte = 1 * self.n * data_type_bytes_c
            with open(work_dir + "/input/bias_gm.bin", 'wb') as file:
                file.truncate(file_byte)